package demo.vo;

/**
 * 范围
 * 
 * @author hanjy
 *
 * @param <T>
 */
public class Range<T extends Comparable<T>> {

    /** 最小时间 */
    private T min;
    /** 最大时间 */
    private T max;

    @Override
    public String toString() {
        return "Range [min=" + min + ", max=" + max + "]";
    }

    public Range() {
        super();
    }

    public Range(T min, T max) {
        super();
        this.min = min;
        this.max = max;
    }

    /**
     * 拓展时间范围
     * 
     * @param target
     */
    public void merge(Range<T> that) {
        if (that == null) {
            return;
        }
        T tmin = that.getMin();
        T smin = this.getMin();
        boolean ok = tmin != null && (smin == null || tmin.compareTo(smin) < 0);
        if (ok) {
            this.setMin(tmin);
        }
        T tMax = that.getMax();
        T sMax = this.getMax();
        ok = tMax != null && (sMax == null || tMax.compareTo(sMax) > 0);
        if (ok) {
            this.setMax(tMax);
        }
    }

    public static void main(String[] args) {
        Range<Integer> range1 = new Range<Integer>(null, 3);
        Range<Integer> range2 = new Range<Integer>(1, null);
        range1.merge(range2);
        System.out.println(range1);
    }

    public T getMin() {
        return min;
    }

    public void setMin(T min) {
        this.min = min;
    }

    public T getMax() {
        return max;
    }

    public void setMax(T max) {
        this.max = max;
    }
}
